<?php

/**
 * @author pigsunny
 * @copyright 2011
 */
 
require_once("./interface.php");
require_once("./global.php");

function changeHeroProperty($cid,$hid,$state,$tocity=false,$tocid="0"){
    if ($tocity){
        sql_query("update sys_city_hero set cid=$tocid,state=$state where cid=$cid and hid=$hid");
    }
    else{
        sql_query("update sys_city_hero set state=$state where cid=$cid and hid=$hid");
    }
}

function createReportContent($origincid,$happencid,$task,$state){
    if($origincid>0)
	{
		$origincity=sql_fetch_one_cell("select name from sys_city where cid='$origincid'");
		if (empty($origincity))
		{
			$origincity = sql_fetch_one_cell("select c.name from mem_world m left join cfg_world_type c on c.type=m.type where m.wid=".cid2wid($origincid));
		}
	}
	else $origincity="";

	if($origincid==$targetcid)
	{
		$happencity=$origincity;
	}
	else if($happencid>0)
	{
		$happencity=sql_fetch_one_cell("select name from sys_city where cid='$happencid'");
		if (empty($happencity))
		{
			$happencity = sql_fetch_one_cell("select c.name from mem_world m left join cfg_world_type c on c.type=m.type where m.wid=".cid2wid($happencid));
		}
	}
	else $happencity="";
    $x=getCityX($happencid);
    $y=getCityY($happencid);
    
    if ($state==0){
        $content="向".$happencity."（".$x.",".$y."）执行派遣任务的军队已到达目标。<br/>派遣完成。<br/>军队进入".$happencity."（".$x.",".$y."）。";
    }
    if ($state==1){
        switch ($task){
            case 0:{$content="向".$happencity."(".$x.",".$y.")执行运输的军队已返回";break;}
            case 1:{$content="向".$happencity."(".$x.",".$y.")执行派遣的军队已返回";break;}
            case 2:{$content="向".$happencity."(".$x.",".$y.")执行侦察的军队已返回";break;}
            case 3:{$content="向".$happencity."(".$x.",".$y.")执行掠夺的军队已返回";break;}
            case 4:{$content="向".$happencity."(".$x.",".$y.")执行占领的军队已返回";break;}
        }
    }
    
    
    return $content;
}

function getCityX($cid){
    return $cid[3].$cid[4].$cid[5];
}

function getCityY($cid){
    return $cid[0].$cid[1].$cid[2];
}

function getReportTitle($task,$state){
    switch ($state) {
        case 0:{
            $ret=3;break;
        }
        case 1:{
            switch ($task){
                case 0:$ret=2;break;
                case 2:$ret=6;break;
                case 3:$ret=8;break;
                case 4:$ret=10;break;
                default:$ret=0;break;
            }break;
        }
    }
    return $ret;
} 


function getPatrolContent($cid,$hid,$targetcid,$targethid,$patrollevel,$haveBattle=false){
    //获取侦察内容
    if (sql_check("select * from sys_city where cid=$targetcid")){
        //目标是城池
        if($patrollevel==0){
                $failinformation="侦察失败！";
            }
                //获取资源
        $res_info=sql_fetch_one("select * from mem_city_resource where cid=$targetcid");
        $information[0]="<table border=0 cellspacing=1 cellpadding=1 bgcolor=\"#FFFFFF\">
                                    <tr>
                                        <td bgcolor=\"#17292B\"><strong>资源</strong></td>
                                        <td bgcolor=\"#17292B\"><strong>数量</strong></td>
                                    </tr>
                                    <tr>
		                              <td bgcolor=\"#17292B\">粮食</td>
		                              <td bgcolor=\"#17292B\">".(int)$res_info[food]."</td>
                                    </tr>
                                    <tr>
                                        <td bgcolor=\"#17292B\">木材</td>
                                        <td bgcolor=\"#17292B\">".(int)$res_info[wood]."</td>
	                               </tr>
	                               <tr>
		                              <td bgcolor=\"#17292B\">石料</td>
		                              <td bgcolor=\"#17292B\">".(int)$res_info[rock]."</td>
	                               </tr>
	                               <tr>
		                              <td bgcolor=\"#17292B\">铁锭</td>
		                              <td bgcolor=\"#17292B\">".(int)$res_info[iron]."</td>
	                               </tr>";
        if ($patrollevel>=3){
            $information[0]=$information[0]."<tr>
                        <td bgcolor=\"#17292B\">黄金</td>
                        <td bgcolor=\"#17292B\">".(int)$res_info[gold]."</td>
                    </tr>
	               <tr>
		              <td bgcolor=\"#17292B\">人口</td>
		              <td bgcolor=\"#17292B\">".$res_info[people]."</td>
	               </tr>
	               <tr>
		              <td bgcolor=\"#17292B\">民心</td>
		              <td bgcolor=\"#17292B\">".$res_info[morale]."</td>
	               </tr>";
            }
        $information[0]=$information[0]."</table>";

                //获取建筑
        $information[1]="<table border=0 cellspacing=1 cellpadding=1 bgcolor=\"#FFFFFF\">
	                               <tr>
		                              <td bgcolor=\"#17292B\"><strong>建筑</strong></td>
                                      <td bgcolor=\"#17292B\"><strong>等级</strong></td>
	                               </tr>
		                          <tr>
		                              <td bgcolor=\"#17292B\">农田</td>
		                              <td bgcolor=\"#17292B\">";
        $building_info=sql_fetch_rows("select * from sys_building where cid=$targetcid and bid=1");
        foreach($building_info as $bulding){
            $information[1]=$information[1].$bulding[level].",";
        }
                
        $building_info=sql_fetch_rows("select * from sys_building where cid=$targetcid and bid=1");
        foreach($building_info as $bulding){
            $information[1]=$information[1].$bulding[level].",";
        }
        $information[1]=$information[1]."</td></tr>
	                                           <tr>
		                                          <td bgcolor=\"#17292B\">伐木场</td>
		                                          <td bgcolor=\"#17292B\">";
        $building_info=sql_fetch_rows("select * from sys_building where cid=$targetcid and bid=2");
        foreach($building_info as $bulding){
            $information[1]=$information[1].$bulding[level].",";
        }
        $information[1]=$information[1]."</td></tr>
	                                       <tr>
		                                      <td bgcolor=\"#17292B\">采石场</td>
		                                      <td bgcolor=\"#17292B\">";
        $building_info=sql_fetch_rows("select * from sys_building where cid=$targetcid and bid=3");
        foreach($building_info as $bulding){
            $information[1]=$information[1].$bulding[level].",";
        }
        $information[1]=$information[1]."</td></tr>
	                                       <tr>
		                                      <td bgcolor=\"#17292B\">铁矿</td>
		                                      <td bgcolor=\"#17292B\">";
        $building_info=sql_fetch_rows("select * from sys_building where cid=$targetcid and bid=4");
        foreach($building_info as $bulding){
            $information[1]=$information[1].$bulding[level].",";
        }
        $information[1]=$information[1]."</td></tr>";
        if ($patrollevel>=4){
            $information[1]=$information[1]."</td></tr>
	                                       <tr>
		                                      <td bgcolor=\"#17292B\">民房</td>
		                                      <td bgcolor=\"#17292B\">";
            $building_info=sql_fetch_rows("select * from sys_building where cid=$targetcid and bid=5");
            foreach($building_info as $bulding){
                $information[1]=$information[1].$bulding[level].",";
            }
            $information[1]=$information[1]."</td></tr>";
            $building_info=sql_fetch_one_cell("select level from sys_building where cid=$targetcid and bid=6");
            $information[1]=$information[1]."<tr><td bgcolor=\"#17292B\">官府</td>
                        <td bgcolor=\"#17292B\">".$building_info."</td></tr>";
            $building_info=sql_fetch_one_cell("select level from sys_building where cid=$targetcid and bid=7");
            $information[1]=$information[1]."<tr><td bgcolor=\"#17292B\">书院</td>
                        <td bgcolor=\"#17292B\">".$building_info."</td></tr>";
            $building_info=sql_fetch_one_cell("select level from sys_building where cid=$targetcid and bid=8");
            $information[1]=$information[1]."<tr><td bgcolor=\"#17292B\">校场</td>
                        <td bgcolor=\"#17292B\">".$building_info."</td></tr>";
            $building_info=sql_fetch_rows("select level from sys_building where cid=$targetcid and bid=9");
            $information[1]=$information[1]."<tr><td bgcolor=\"#17292B\">军营</td>
                        <td bgcolor=\"#17292B\">";
            foreach($building_info as $bulding){
                $information[1]=$information[1].$bulding[level].",";
            }
            $information[1]=$information[1]."</td></tr>";
            $building_info=sql_fetch_one_cell("select level from sys_building where cid=$targetcid and bid=10");
            $information[1]=$information[1]."<tr><td bgcolor=\"#17292B\">客栈</td>
                        <td bgcolor=\"#17292B\">".$building_info."</td></tr>";
            $building_info=sql_fetch_one_cell("select level from sys_building where cid=$targetcid and bid=11");
            $information[1]=$information[1]."<tr><td bgcolor=\"#17292B\">招贤馆</td>
                        <td bgcolor=\"#17292B\">".$building_info."</td></tr>";
            $building_info=sql_fetch_one_cell("select level from sys_building where cid=$targetcid and bid=12");
            $information[1]=$information[1]."<tr><td bgcolor=\"#17292B\">鸿胪寺</td>
                        <td bgcolor=\"#17292B\">".$building_info."</td></tr>";
            $building_info=sql_fetch_one_cell("select level from sys_building where cid=$targetcid and bid=13");
            $information[1]=$information[1]."<tr><td bgcolor=\"#17292B\">市场</td>
                        <td bgcolor=\"#17292B\">".$building_info."</td></tr>";
            $building_info=sql_fetch_one_cell("select level from sys_building where cid=$targetcid and bid=14");
            $information[1]=$information[1]."<tr><td bgcolor=\"#17292B\">铁匠铺</td>
                        <td bgcolor=\"#17292B\">".$building_info."</td></tr>";
            $building_info=sql_fetch_one_cell("select level from sys_building where cid=$targetcid and bid=15");
            $information[1]=$information[1]."<tr><td bgcolor=\"#17292B\">工匠作坊</td>
                        <td bgcolor=\"#17292B\">".$building_info."</td></tr>";
            $building_info=sql_fetch_one_cell("select level from sys_building where cid=$targetcid and bid=16");
            $information[1]=$information[1]."<tr><td bgcolor=\"#17292B\">马厩</td>
                        <td bgcolor=\"#17292B\">".$building_info."</td></tr>";
            $building_info=sql_fetch_one_cell("select level from sys_building where cid=$targetcid and bid=17");
            $information[1]=$information[1]."<tr><td bgcolor=\"#17292B\">仓库</td>
                        <td bgcolor=\"#17292B\">".$building_info."</td></tr>";
            $building_info=sql_fetch_one_cell("select level from sys_building where cid=$targetcid and bid=18");
            $information[1]=$information[1]."<tr><td bgcolor=\"#17292B\">驿站</td>
                        <td bgcolor=\"#17292B\">".$building_info."</td></tr>";
            $building_info=sql_fetch_one_cell("select level from sys_building where cid=$targetcid and bid=19");
            $information[1]=$information[1]."<tr><td bgcolor=\"#17292B\">烽火台</td>
                        <td bgcolor=\"#17292B\">".$building_info."</td></tr>";
            $building_info=sql_fetch_one_cell("select level from sys_building where cid=$targetcid and bid=20");
            $information[1]=$information[1]."<tr><td bgcolor=\"#17292B\">城墙</td>
                        <td bgcolor=\"#17292B\">".$building_info."</td></tr>";
            }
        $information[1]=$information[1]."</table>";
        
        $information[2]="";
        
        $information[3]="";
   
        //获取城防
        $information[4]="";

        //获取军队模糊
        $information[5]="";
        
        //获取军队详细
        $information[6]="";
        
        //获取将等级
        $information[7]="";
          
        //获取科技
        $information[8]="";
        
        //获取动态
        $information[9]="";
    }
    else{
        //目标是野地    
        switch ($patrollevel){
            case 0:{
                $ret="侦察失败！";
            }
        }
    }
    
    $happencity=sql_fetch_one_cell("select name from sys_city where cid='$targetcid'");
    if (empty($happencity))
    {
        $happencity = sql_fetch_one_cell("select c.name from mem_world m left join cfg_world_type c on c.type=m.type where m.wid=".cid2wid($happencid));
    }
    $x=getCityX($targetcid);
    $y=getCityY($targetcid);
    $ret="向".$happencity."（".$x."，".$y."）执行侦察任务的军队已到达目标。<br/>";
    if ($haveBattle){
        $ret=$ret."对方出城迎战。<br/>";
    }
    else {
        $ret[]=$ret."对方未发觉。<br/>";
    }
    $ret=$ret."侦察报告：<br/>";
    for ($i=0;$i<$patrollevel;$i++){
        $ret=$ret.$information[$i];
    }
    $ret=$ret."侦察完成。<br/>
         <table width=\"567\" border=0 cellpadding=1 cellspacing=1 bgcolor=\"#FFFFFF\">
        <tr>
            <td height=\"25\" colspan=\"2\" align=\"center\" class=\"TitleBlueWhite\">部队返回</td>
        </tr>
        <tr>
            <td height=\"55\" bgcolor=\"#17292B\" class=\"TitleListWhite\">
     <table width=\"255\" border=\"0\" align=\"center\" cellspacing=\"0\" bgcolor=\"#17292B\">
        <tr>
            <td width=\"131\" height=\"40\"><img name=\"\" src=\"/images/herox/hero_boy_1034.jpg\" width=\"30\" height=\"36\" alt=\"蒙水明\" /></td>
            <td width=\"120\" align=\"center\" class=\"NameBlue\">蒙水明 （等级30）</td>
        </tr>
    </table>
    </td>
    </tr>
    <tr>
    <td height=\"25\" colspan=\"2\" align=\"center\" class=\"TextArmyCount\">
    <table width=\"249\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
      <tr class=\"TitleBattleYellow\">
        <td width=\"120\" height=\"25\" align=\"center\" valign=\"middle\">军队</td>
        <td width=\"129\" height=\"25\" align=\"center\" valign=\"middle\">数量</td>
        </tr>

	<tr>
        <td width=\"120\" height=\"25\" align=\"center\" valign=\"middle\" class=\"TextArmyCount\">斥候</td>
        <td width=\"129\" height=\"25\" align=\"center\" valign=\"middle\" class=\"TextArmyCount\">10</td>
    </tr>
    </table>
    </td>
  </tr>

</table>

军队正在返回雄霸天下（211，200）。<br/>";

    
    return $ret;
}

function getPatrolLevel($cid,$targetcid,$hid,$soldiers){
        //获取双方斥候数量
        $soldierArray=explode(",",$soldiers);
        $attNum=$soldierArray[2];
        $defNum=sql_fetch_one_cell("select count from sys_city_soldier where cid=$targetcid and sid=3");
        
        //英雄统帅、内政、勇武、智谋，玩家的宝物状态，城池的科技等级，TMD东西还真多
        $AttackCommand=getCommandAdd($cid,$hid);//获取统帅加成,返回实际数量
        $attAttack=getAttackAdd($cid,$hid);//获取攻击加成
        $attDefence=getDefenceAdd($cid,$hid);//获取防御加成
        $attSpeed=getSpeed($cid,$hid);//获取速度加成
        $attHealth=getHealthAdd($cid,$hid);//获取生命加成
        //目标有将的话，先判断是否有主将，没有的话城守出战
        $haveGeneral=sql_check("select * from sys_city_hero where cid=$targetcid and state=7");//主将判断
        $haveGuard=sql_check("select hid from sys_city_hero where cid=$targetcid and state=1");//城守判断
        if ($haveGeneral){
            //有主将
            $targethid=sql_fetch_one_cell("select hid from sys_city_hero where cid=$targetcid and state=7");
            $defCommand=getCommandAdd($targetcid,$targethid);
            $defAttack=getAttackAdd($targetcid,$targethid);
            $defDefence=getDefenceAdd($targetcid,$targethid);
            $defSpeed=getSpeed($targetcid,$targethid);
            $defHealth=getHealthAdd($targetcid,$targethid);
        }
        if (!$haveGeneral&&$haveGuard){
            //没主将，有城守
            $targethid=sql_fetch_one_cell("select hid from sys_city_hero where cid=$targetcid and state=1");
            $defCommand=getCommandAdd($targetcid,$targethid);
            $defAttack=getAttackAdd($targetcid,$targethid);
            $defDefence=getDefenceAdd($targetcid,$targethid);
            $defSpeed=getSpeed($targetcid,$targethid);
            $defHealth=getHealthAdd($targetcid,$targethid);
        }
        if (!$haveGeneral&&!$haveGuard){
            //没有主将，没有城守
            $defCommand=0;
            $defAttack=0;
            $defDefence=0;
            $defSpeed=0;
            $defHealth=0; 
        }
        if ($attSpeed>$defSpeed){
        //进攻方先行动
            $defLeft=$defNum*100*(1+$defHealth/500)*(1+min(1,$defCommand/$defNum))
                +$defNum*20*(1+$defDefence/100)*(1+min(1,$defCommand/$defNum))
                -$attNum*20*(1+$attAttack/100)*(1+min(1,$AttackCommand/$attNum));
            if ($defLeft<0){
                $retLevel=sql_fetch_one_cell("select level from sys_city_technic where cid=$cid and tid=7");
                $defLeft=0;
                $attLeft=$attNum;
            }
            else {
                $attLeft=$attNum*(1+100*$attHealth/500)*(1+min(1,$AttackCommand/$attNum))
                    +$attNum*20*(1+$attDefence/100)*(1+min(1,$AttackCommand/$attNum))
                    -$defLeft/100*20*(1+$defAttack/100)*(1+min(1,$defCommand/$defLeft/100));
                if ($attLeft<0){
                    $retLevel=0;
                    $attLeft=0;
                    $defLeft=$defLeft/100;
                }
                else {
                    $level=sql_fetch_one_cell("select level from sys_city_technic where cid=$cid and tid=7");
                    $retLevel=(int)($level*$attLeft/100/$attNum);
                    $attLeft=$attLeft/100;
                    $defLeft=$defLeft/100;
                }
            }
        }
        else {
            //防守方先行动
            $attLeft=$attNum*100*(1+$attHealth/500)*(1+min(1,$AttackCommand/$attNum))
            +$attNum*20*(1+$attDefence/100)*(1+min(1,$AttackCommand/$attNum))
            -$defNum/100*20*(1+$defAttack/100)*(1+min(1,$defCommand/$defNum/100));
            if ($attLeft<0){
                $retLevel=0;
                $attLeft=$attLeft/100;
                $defLeft=$defNum;
            }
            else {
                $defLeft=$defNum*100*(1+$defHealth/500)*(1+min(1,$defCommand/$defNum))
                +$defNum*20*(1+$defDefence/100)*(1+min(1,$defCommand/$defNum))
                -$attLeft/100*20*(1+$attAttack/100)*(1+min(1,$AttackCommand/$attLeft/100));
                $level=sql_fetch_one_cell("select level from sys_city_technic where cid=$cid and tid=7");
                $retLevel=(int)($level*$attLeft/100/$attNum);
                $attLeft=$attLeft/100;
                $defLeft=$defLeft/100;
            }
        }
        $ret[]=$retLevel;
        $ret[]=$attNum;
        $ret[]=$attLeft;
        $ret[]=$defNum;
        $ret[]=$defLeft;
        $ret[]=$targethid;
        return $ret;
}

function getCommandAdd($cid,$hid){
    //科技、将领、宝物
    $heroCommand=sql_fetch_one_cell("select level+command_base+command_add_on from sys_city_hero where hid=$hid and cid=$cid");
    $tecCommand=sql_fetch_one_cell("select level from sys_city_technic where cid=$cid and tid=6");
    $heroBuf=sql_check("select * from mem_hero_buffer where hid=$hid and buftype=1 and endtime>unix_timestamp()");
    return $heroCommand*(1+$tecCommand/10+0.5*$heroBuf)*100;
}

function getAttackAdd($cid,$hid){
    //科技、将领、宝物
    $heroAttack=sql_fetch_one_cell("select bravery_base+bravery_add+bravery_add_on from sys_city_hero where hid=$hid and cid=$cid");
    $tecAttack=sql_fetch_one_cell("select level from sys_city_technic where cid=$cid and tid=9");
    $goodsBuf=sql_check("SELECT * , c.uid FROM mem_user_buffer u LEFT JOIN sys_city c ON u.uid = c.uid WHERE c.cid =$cid and u.buftype=5");
    $heroBuf=sql_check("select * from mem_hero_buffer where hid=$hid and buftype=3 and endtime>unix_timestamp()");
    return $heroAttack*(1+$tecAttack/10+$goodsBuf*0.1+$heroBuf*0.25);
}

function getDefenceAdd($cid,$hid){
    //科技、将领、宝物
    $heroDefence=sql_fetch_one_cell("select wisdom_base+wisdom_add+wisdom_add_on from sys_city_hero where hid=$hid and cid=$cid");
    $tecDefence=sql_fetch_one_cell("select level from sys_city_technic where cid=$cid and tid=10");
    $goodsBuf=sql_check("SELECT * , c.uid FROM mem_user_buffer u LEFT JOIN sys_city c ON u.uid = c.uid WHERE c.cid =$cid and u.buftype=6");
    $heroBuf=sql_check("select * from mem_hero_buffer where hid=$hid and buftype=4 and endtime>unix_timestamp()");
    return $heroDefence*(1+$tecDefence/10+$goodsBuf*0.1+$heroBuf*0.25);
}

function getHealthAdd($cid,$hid){
    $heroHealth=sql_fetch_one_cell("select affairs_base+affairs_add+affairs_add_on from sys_city_hero where hid=$hid and cid=$cid");
    $tecHealth=sql_fetch_one_cell("select level from sys_city_technic where cid=$cid and tid=16");
    $heroBuf=sql_check("select * from mem_hero_buffer where hid=$hid and buftype=2 and endtime>unix_timestamp()");
    return $heroHealth*(1+$tecHealth/10+$heroBuf*0.25);
}

function getSpeed($cid,$hid){
    return sql_fetch_one_cell("select speed_add_on from sys_city_hero where hid=$hid and cid=$cid");
}

function getDefenceTroops($cid,$task){
    $uid=sql_fetch_one_cell("select uid from sys_city where cid=$cid");
    $soldiers="";
    $people=0;
    if ($task==3){
        $tactics=sql_fetch_one_cell("select deplunder_join from sys_city_tactics where cid=$cid");
    }
    else {
        $tactics=sql_fetch_one_cell("select deinvade_join from sys_city_tactics where cid=$cid");
    }    
    if ($tactics){
        $soldierArray=explode(",",$tactics);
        $s_count=0;
        foreach ($soldierArray as $sid){
            $num=sql_fetch_one_cell("select count from sys_city_soldier where cid=$cid and sid=$sid");
            if ($num!=0) {
                $soldiers=$soldiers.$sid.",".$num.",";
                $s_count++;
            }
            $people_need=sql_fetch_one_cell("select people_need from cfg_soldier where sid=$sid");
            $people=$people+$num*$people_need;
        }
        $soldiers=$s_count.",".$soldiers;
    }
    else{
        $soldierArray=sql_fetch_rows("select * from sys_city_soldier where cid=$cid order by sid");
        $s_count=0;
        foreach ($soldierArray as $soldier){
            $soldiers=$soldiers.$soldier[sid].",".$soldier[count].",";
            $people_need=sql_fetch_one_cell("select people_need from cfg_soldier where sid=$soldier[sid]");
            $people=$people+$num*$people_need;
            $s_count++;
        }
        $soldiers=$s_count.",".$soldiers;
    }
    $haveGeneral=sql_fetch_one_cell("select hid from sys_city_hero where cid=$cid and state=7");//主将判断
    $haveGuard=sql_fetch_one_cell("select hid from sys_city_hero where cid=$cid and state=1");//城守判断
    if ($haveGeneral){
    //有主将
        $hid=$haveGeneral;
    }
    if (!$haveGeneral&&$haveGuard){
    //没主将，有城守
        $hid=$haveGuard;
    }
    if (!$haveGeneral&&!$haveGuard){
    //没有主将，没有城守
        $hid=0;
    }
    //file_put_contents("log.txt",$uid.",".$cid.",".$hid.";".$soldiers.";".$people."\r\n",FILE_APPEND);
    sql_query("insert into sys_troops(uid,cid,hid,targetcid,task,state,starttime,soldiers,people) values ($uid,$cid,$hid,$cid,'5','3',unix_timestamp(),'$soldiers',$people)");
    //sql_query("insert into sys_troops(uid,cid,hid,targetcid,task,state,starttime,soldiers,people) values ('6915','157095','159137','157095','5','3',unix_timestamp(),'2,21,7,173000,','519021')");
    $ret[]=$hid;
    $ret[]=$soldiers;
    return $ret;
}

function getBattleRange($att_cid,$att_soldiers,$def_cid,$def_soldiers){
    $range=270;
    //file_put_contents("log.txt",$att_cid.";".$att_soldiers.";".$def_cid.";".$def_soldiers."\r\n",FILE_APPEND);
    $soldiers=explode(",",$att_soldiers);
    $tec=sql_fetch_one_cell("select level from sys_city_technic where cid=$att_cid and tid=14");
    for ($i=1;$i<count($soldiers)-1;$i=$i+2){
        $sid=$soldiers[$i];
        //file_put_contents("log.txt",$sid.";\r\n",FILE_APPEND);
        $s_range=sql_fetch_one_cell("select range from cfg_soldier where sid=$sid");
        if ((($s_range+399)*(1+$tec/10))>$range){
            $range=($s_range+399)*(1+$tec/10);
        }
    }
    $soldiers=explode(",",$def_soldiers);
    $tec=sql_fetch_one_cell("select level from sys_city_technic where cid=$def_cid and tid=14");
    for ($i=1;$i<count($soldiers)-1;$i=$i+2){
        $sid=$soldiers[$i];
        $s_range=sql_fetch_one_cell("select range from cfg_soldier where sid=$sid");
        if ((($s_range+399)*(1+$tec/10))>$range){
            $range=($s_range+399)*(1+$tec/10);
        }
    }
    return $range;
}

function createNpcSoldiers($uid,$cid){ 
    //生成随即部队，util里面的方法看不太懂
    $level=sql_fetch_one_cell("select level from mem_world where wid=".cid2wid($cid));
    $num=pow(2,$level);
    $npcValue=sql_fetch_one_cell("select npcvalue from cfg_city_npcvalue where level=$level");
    $hid=sql_fetch_one_cell("select hid from sys_city_hero where cid=$cid");
    if (!$hid){
        $hid=0;
    }
    if ($uid==894){
        for ($i=0;$i<5;$i++){
            $sid=18;
            $have=rand(0,1);
            if ($have){
                if ($sid==22) $count=$npcValue;
                else {
                    $count=rand(1,$npcValue);
                }
                $soldiers=$soldiers.$sid.",".$count.",";
                $npcValue=$npcValue-$count;
                $sid++;
            }
            else {
                $sid++;
            }
        }
        
    }
    else{
        for ($i=0;$i<10;$i++){
            $sid=1;
            if ($sid==10) $count=$npcValue;
            else {
                $count=rand(1,$npcValue);
            }
            $soldiers=$soldiers.$sid.",".$count.",";
            $npcValue=$npcValue-$count;
            $sid++;
        } 
    }
    $ret[]=$hid;
    $ret[]=$soldiers;
    return $ret;
}

function setSoldierPos($soldiers,$pos){
    $soldier_array=explode(",",$soldiers);
    $ret=$soldier_array[0].",";
    for ($i=1;$i<=$soldier_array[0];$i++){
        $ret=$ret[2*$i-1].",".$pos.",";
    }
    return $ret;
}

function setTactics($att_soldiers,$def_soldiers){
    $att_soldier_array=explode(",",$att_soldiers);
    for ($i=1;$i<=$att_soldier_array[0];$i++){
        
    }
}

function updateUserTechnic($uid){
    //TO DO 更新用户的科技
    $cities=sql_fetch_rows("SELECT cid FROM sys_city WHERE uid=".$uid);
    foreach($cities as $city){
        $technics=sql_fetch_rows("SELECT * FROM sys_technic WHERE uid=".$uid);
        foreach($technics as $technic){
            sql_query("REPLACE INTO sys_city_technic VALUES (".$city["cid"].",".$technic["tid"].",".$technic["level"].")");
        }
    }
}




 


?>